package com.five.calendar.function.goals;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.five.calendar.ApplicationHelper;
import com.five.calendar.R;
import com.five.calendar.base.BaseFragment;
import com.five.calendar.common.observer.Observer;
import com.five.calendar.database.bean.TargetBean;
import com.five.calendar.database.bean.TargetDayBean;
import com.five.calendar.database.observer.TargetDayObservable;
import com.five.calendar.database.observer.TargetObservable;
import com.five.calendar.ui.activity.GoalDoneShowActivity;
import com.five.calendar.ui.adapter.GoalListAdapter;
import com.five.calendar.ui.widget.GoalRecyclerView;
import com.five.calendar.ui.widget.OnItemClickListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @desc:
 * @auther:duwei
 * @date:2017/8/30
 */

public class GoalsFragment extends BaseFragment {


    private GoalRecyclerView mRecycleView;
    private String[] mTimes;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.calendar_goals,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecycleView = (GoalRecyclerView) view.findViewById(R.id.goal_show_list);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("ZXX","goal fragment onResume");
        TargetObservable.queryTarget().registerObserver(new Observer<TargetBean, Integer, List<TargetBean>>() {
            @Override
            public void onFail(Integer integer) {

            }

            @Override
            public void onComplete(final List<TargetBean> targetBeen) {
                if(targetBeen != null){
                    Collections.sort(targetBeen);
                    final GoalListAdapter adapter = new GoalListAdapter(getContext(), targetBeen);
                    mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
                    mRecycleView.setAdapter(adapter);
                    mRecycleView.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, final int position) {
                            TargetDayObservable.queryTargetByTargetId(targetBeen.get(position).id).registerObserver(new Observer<TargetDayBean, Integer, List<TargetDayBean>>() {
                                @Override
                                public void onFail(Integer integer) {

                                }

                                @Override
                                public void onComplete(List<TargetDayBean> targetDayBeen) {
                                    Intent intent =  new Intent(getContext(), GoalDoneShowActivity.class);
                                    Bundle bundle = new Bundle();
                                    if (targetDayBeen != null){
                                        Log.e("ZXX", "targetDayBean.size: " + targetDayBeen.size());
                                        mTimes = new String[targetDayBeen.size()];
                                        for (int i = 0; i < targetDayBeen.size(); i++){
                                            Log.e("ZXX", targetDayBeen.get(i).time);
                                            mTimes[i] = targetDayBeen.get(i).time;
                                        }
                                        Log.e("ZXX", mTimes.toString());
                                    } else {
                                        mTimes = null;
                                    }
                                    bundle.putStringArray("times", mTimes);
                                    bundle.putSerializable("targetbean", targetBeen.get(position));
                                    intent.putExtras(bundle);
                                    getActivity().startActivityForResult(intent, 2);
                                }

                                @Override
                                public void onSuccess(TargetDayBean targetDayBean) {
                                }
                            }).startDataHandle();
                        }

                        @Override
                        public void onDoneClick(int position) {
                            TargetBean targetBeans = targetBeen.get(position);
                            if (targetBeans.isComplete == 0) {
                                targetBeans.isComplete = 1;
                            } else {
                                targetBeans.isComplete = 0;
                            }

                            TargetObservable.updateTarget(targetBeans).registerObserver(new Observer<TargetBean, Integer, List<TargetBean>>() {
                                @Override
                                public void onFail(Integer integer) {

                                }

                                @Override
                                public void onComplete(List<TargetBean> targetBeen) {

                                }

                                @Override
                                public void onSuccess(TargetBean targetBean) {
                                    Collections.sort(targetBeen);
                                    adapter.notifyDataSetChanged();
                                }
                            }).startDataHandle();
                        }
                    });
                } else {
                    List<TargetBean> stargetBeen = new ArrayList<TargetBean>();
                    final GoalListAdapter adapter = new GoalListAdapter(getContext(), stargetBeen);
                    mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
                    mRecycleView.setAdapter(adapter);
                }
            }

            @Override
            public void onSuccess(TargetBean targetBean) {

            }
        }).startDataHandle();
    }
}
